package com.jcw.kqxt.controller;

import com.jcw.kqxt.customAnnotation.PermissionVerification;
import com.jcw.kqxt.domain.stu;
import com.jcw.kqxt.query.DeptQuery;
import com.jcw.kqxt.result.JSONResult;
import com.jcw.kqxt.result.PageList;
import com.jcw.kqxt.service.IDeptService;
import com.jcw.kqxt.domain.Dept;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * @desc
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    public IDeptService deptService;

    /**
     * 保存
     * @param dept 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    @Transactional
    @PermissionVerification(auth = "dept:save")
    public JSONResult save(@RequestBody Dept dept){
       // dept.setPId(1L);
//        Dept dept = new Dept();
//        dept.setPId(stu.getpId());
//        dept.setName(stu.getName());
        deptService.insert(dept);
        return JSONResult.success();
    }
    /**
     * 修改
     * @param dept 前端传递来的实体数据
     */
    @PostMapping(value="/update")
    @Transactional
    @PermissionVerification(auth = "dept:update")
    public JSONResult update(@RequestBody Dept dept){
        deptService.updateById(dept);
        return JSONResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    @Transactional
    @PermissionVerification(auth = "dept:delete:id")
    public JSONResult delete(@PathVariable("id") Long id){
            deptService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    @PermissionVerification(auth = "dept:select:id")
    public JSONResult get(@PathVariable("id")Long id)
    {
        return JSONResult.success(deptService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    @PermissionVerification(auth = "dept:list")
    public JSONResult list(){
        return JSONResult.success(deptService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    @PermissionVerification(auth = "dept:pagelist")
    public JSONResult pageList(@RequestBody DeptQuery query)
    {
        Page<Dept> page = new Page<Dept>(query.getPage(),query.getRows());
        page = deptService.selectPage(page);
        return JSONResult.success(new PageList<Dept>(page.getTotal(), page.getRecords()));
    }

    /**
     *
     * @return 部门树
     */
    @GetMapping("/tree")
    @PermissionVerification(auth = "dept:tree")
    public JSONResult deptTree(){
        return deptService.deptTree();
    }

    /**
     * 停用部门
     * @param id
     * @return
     */
    @PostMapping("/disable/{id}")
    @PermissionVerification(auth = "dept:disable:id")
    public JSONResult deptDisable(@PathVariable("id") Long id){
        return deptService.deptDisable(id);
    }
}